package com.dmy.chat.server.netty.coder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.dmy.chat.server.netty.message.Messages;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.util.List;

/**
 * create by huahuadelei for 2020/1/15
 */
public class WebSocketEncoder extends MessageToMessageEncoder<Messages> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Messages messages, List<Object> list) throws Exception {
        list.add(JSON.toJSONString(messages, SerializerFeature.WriteMapNullValue));
    }

    public String encode(Object msg){
        return JSON.toJSONString(msg, SerializerFeature.WriteMapNullValue);
    }
}
